(function($) {	
    function celebrioTaskbar(path) {
		
        this.createClientTaskbarIcons = function() {
            taskbar.createDefaultTaskbarIcons();
			
            $("#taskbar").append(
                '<div id="wifi" class="taskbaricon">'
                + '<img src="'+ path +'/wifi/' + dpi + 'dpi/3.png" border="0" />'
                + '</div>');
            $("#taskbar").append(
                '<div id="gsm" class="taskbaricon">'
                + '<img src="'+ path +'/antenna/' + dpi + 'dpi/4.png" border="0" />'
                + '</div>');
            $("#taskbar").append(
                '<div id="speaker" class="taskbaricon">'
                + '<img src="'+ path +'/speaker/' + dpi + 'dpi/4.png" border="0" />'
                + '</div>');
            $("#taskbar").append(
                '<div id="battery" class="taskbaricon">'
                + '<img src="'+ path +'/battery/' + dpi + 'dpi/4.png" border="0" />'
                + '</div>');
				
            $(".taskbaricon").bind('dragstart', function(event) {
                event.preventDefault();
            });
        }
		
        this.createDefaultTaskbarIcons = function() {	
            /**
			 * var logoutPath - global variable defined in WebtopModule/templates/@layout.phtml
			 */
			
            $("#taskbar").append(
                $('<div id="logout" class="specialtaskbaricon">').append(
                    '<a href="' + logoutPath + '"><img src="' + path + '/logout/' + dpi + 'dpi/icon.png" border="0" /></a>'
                    )
                ).bind('dragstart', function(event) {
                event.preventDefault();
            });
        }
		
        this.layOutTaskbarIcons = function() {
            var taskspacePosition = $("#taskspace").position().left;
            var iconPosition = $("#taskspace").width() / 5;
            var iconWidth = $("#logout").outerWidth(true) / 2;
		
            $("#logout").css("left", (iconPosition*2.5 - iconWidth + taskspacePosition));
            $("#wifi").css("left", (iconPosition*0.5 - iconWidth + taskspacePosition));
            $("#gsm").css("left", (iconPosition* 1.5 - iconWidth + taskspacePosition));
            $("#speaker").css("left", (iconPosition*3.5 - iconWidth + taskspacePosition));
            $("#battery").css("left", (iconPosition*4.5 - iconWidth + taskspacePosition));
    		
            if($("#taskspace").width() < iconWidth*10)
                $(".taskbaricon").css("visibility", "hidden");
            else 
                $(".taskbaricon").css("visibility", "visible");
        }
		
        this.showTaskbarIcons = function() {		
            $(".taskbaricon").css("visibility", "visible");
            $(".specialtaskbaricon").css("visibility", "visible");
        }
		
        this.refreshSignalIcons = function() {
            wifi = Android.getWifiState();
            gsm = Android.getGsmState();
			
            $("#wifi img").attr("src", path + "/wifi/" + dpi + "dpi/"+ wifi +".png");
            $("#gsm img").attr("src", path + "/antenna/" + dpi + "dpi/"+ gsm +".png");
        }
		
        this.refreshSpeakerIcon = function() {
            speaker = Android.getSpeakerState();
			
            $("#speaker img").attr("src", path + "/speaker/" + dpi + "dpi/"+ speaker +".png");
        }
		
        this.refreshBatteryIcon = function() {
            battery = Android.getBatteryState();
            if(battery == -1)
                battery = "empty";
            $("#battery img").attr("src", path + "/battery/" + dpi + "dpi/"+ battery +".png");
        }
		
        this.createLogoutButton = function() {
            Android.createLogoutButton($("#logout").position().left, window.location.href + "/sign/in");
            $("#logout").css("visibility", "hidden");
        }
        
        this.createFeedbackButton = function() {
            Android.createFeedbackButton();
            $("#avatar").css("visibility", "hidden");
        }
    }
    
    $(document).ready(function() {
        /**
	 *var basePath - global variable defined in system_app_layout.phtml 
	 *var imagePath - global variable defined in system_app_layout.phtml 
	 */
        var path = basePath + imagePath + "/notification"
	
        taskbar = new celebrioTaskbar(path);
		    	
        if (isClient) {
            taskbar.createClientTaskbarIcons();
        }	
        else
            taskbar.createDefaultTaskbarIcons();
        /**
		 *var isAndroidClient - global variable defined in system_app_layout.phtml  
		 */
        if(isAndroidClient){
            setInterval(function(){
                taskbar.refreshSignalIcons();
                taskbar.refreshBatteryIcon();
            }, 1000);
        }
    }); 
	
    $(window).load(function(){
        taskbar.layOutTaskbarIcons();
        taskbar.showTaskbarIcons();
		
        if(isAndroidClient) {
            taskbar.createLogoutButton();
            taskbar.createFeedbackButton();
        }
    });
	
    $(window).resize(function(){
        if (taskbar) {
            taskbar.layOutTaskbarIcons();
        }
    })        
    
})(jQuery);
